package com.system.permissiondemo.common;

import com.system.permissiondemo.entity.SysUser;

import javax.servlet.http.HttpServletRequest;

/**
* @Description:    进程控制（有效缓解高并发），每个进程都可以取到自己的request
* @Author:         Joe
* @CreateDate:     2020/3/27 11:36
*/
public class RequestHolder {

    private static final ThreadLocal<SysUser> userHolder = new ThreadLocal<SysUser>();

    private static final ThreadLocal<HttpServletRequest> requestHolder = new ThreadLocal<HttpServletRequest>();

    //1.添加
    public static void add(SysUser sysUser){
        userHolder.set(sysUser);
    }
    public static void add(HttpServletRequest request){
        requestHolder.set(request);
    }

    //2.获取
    public static SysUser getCurrentUser(){
        return userHolder.get();
    }
    public static HttpServletRequest getCurrentRequest(){
        return requestHolder.get();
    }


    //3.移除（进程结束时）
    public static void remove(){
        userHolder.remove();
        requestHolder.remove();
    }


}
